package sql.show.demo;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {
	private static final int DATABASE_VERSION = 1;
	private static String DATABASENAME ="myDB";
	private static String PRODUCT_TABLE ="myProduct";
	private static final String KEY_ID = "idProduct";
	private static final String KEY_NAME = "nameProduct";
	private static final String KEY_PRICE = "priceProduct";
	Context mContext;
	private ArrayList<ProductList> mProductList = new ArrayList<ProductList>();
	public DataBaseHelper(Context context) {
		super(context, DATABASENAME,null,DATABASE_VERSION);
		mContext=context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		 String CREATE_PRODUCT_TABLE = "CREATE TABLE " + PRODUCT_TABLE + "("
	                + KEY_ID + " TEXT," + KEY_NAME + " TEXT,"
	                + KEY_PRICE + " TEXT" + ")";
		db.execSQL(CREATE_PRODUCT_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS" + PRODUCT_TABLE);
        onCreate(db);
		
	}
	//Add record
    public void addProduct(ProductList productList) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("idProduct", productList.idProduct);
        contentValues.put("nameProduct", productList.nameProduct);
        contentValues.put("priceProduct", productList.priceProduct);
        db.insert(PRODUCT_TABLE, null, contentValues);
        db.close();
    }
 
        // get all products from database
        public ArrayList<ProductList> getAllProducts() {

        	mProductList.clear();

            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery("select * from myProduct", null);
            if (cursor.getCount() != 0) {
                if (cursor.moveToFirst()) {
                    do {
                    	/**
                    	 * Chú ý : chỗ này, mai mốt gặp Khoa sẽ giải thích
                    	 * @author Kypi221
                    	 */
                        ProductList item = new ProductList();

                        item.idProduct = cursor.getString(cursor.getColumnIndex("idProduct"));

                        item.nameProduct = cursor.getString(cursor.getColumnIndex("nameProduct"));

                        item.priceProduct = cursor.getString(cursor.getColumnIndex("priceProduct"));

                        mProductList.add(item);

                    } while (cursor.moveToNext());
                }
            }
            cursor.close();
            db.close();
            return mProductList;
        }



}
